# -*- coding: utf-8 -*-
# @Time    : 2021/8/29 21:43
# @Author  : BrandonGT
# @FileName: code_03_timer.py

import sys
from PyQt5.QtWidgets import *

class bd_pushbutton(QPushButton):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(140, 50)
        self.move(100, 0)
        
    def mousePressEvent(self, evt):
        self.timer_id = self.startTimer(self.ms)
        return super().mousePressEvent(evt)

    def setSec(self, sec):
        self.setText(str(sec))

    def setTimer(self, ms):
        self.ms = ms

    def timerEvent(self, evt):
        count = int(self.text())
        count -= 1
        self.setText(str(count))
        if count <= 0:
            print("终止")
            self.killTimer(self.timer_id)


class Window(QWidget):
    def __init__(self):
        super().__init__()  # 初始化继承的父类内部的__init__
        self.setWindowTitle("的学习")
        self.resize(300, 300)
        self.setup_ui()

    def setup_ui(self):
        self.auto_count()

    def auto_count(self):
        btn = bd_pushbutton()
        btn.setParent(self)
        btn.setSec(5)
        btn.setTimer(50)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())